package ar.edu.uba.fi.jmit.aspecter.monitor.objectCount;

import java.util.Date;
import java.util.List;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;
import ar.edu.uba.fi.jmit.aspecter.AspecterTestCase;
import ar.edu.uba.fi.jmit.aspecter.core.Constants;
import ar.edu.uba.fi.jmit.aspecter.core.ResourceProperties;
import ar.edu.uba.fi.jmit.aspecter.core.WeavingInstrumentor;
import ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.EventPriorityMock;
import ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.MockEvent;
import ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.SenderFactoryMock;

public class ObjectCountInstrumentorTestCase extends AspecterTestCase {

	public void test() throws NotFoundException {
		SenderFactoryMock.clean();
		String className = getNextClassName();
		try {
			WeavingInstrumentor instrumentor = new ObjectCountInstrumentor("ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.MonitorTarget", "ObjectCountInstrumentorTestCase_test");
			CtClass ctClazz = ClassPool.getDefault().getAndRename("ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.ObjectCountTargetObject", className);
			instrumentor.alter(ctClazz);
			Class<?> clazz = ctClazz.toClass();
			Object object = clazz.newInstance();
			clazz.getMethod("createMonitorTarget").invoke(object);
			List<MockEvent> list = SenderFactoryMock.getSenderInstance().getSendedEvents();
			assertEquals(1, list.size());
			MockEvent event = list.get(0);
			assertEquals("ObjectCountInstrumentorTestCase_test", event.getData().get(ResourceProperties.getProperty(Constants.EVENT_MONITOR_NAME_KEY)));
			Date eventDate = (Date) event.getData().get(ResourceProperties.getProperty(Constants.EVENT_MONITOR_DATE));
			assertSimilarDates(new Date(), eventDate);
			assertEquals("ar.edu.uba.fi.jmit.aspecter.monitor.testUtils.MonitorTarget", event.getSource());
			assertEquals(EventPriorityMock.NORMAL, event.getPriority());
		} catch (Exception e) {
			fail(e);
		}
	}
}
